package com.th.therp.service.Impl;

import com.th.therp.pojo.vo.DashboardMonthVo;
import com.th.therp.pojo.vo.DashboardOrderVo;
import com.th.therp.service.DashboardService;
import com.th.therp.service.PurchaseService;
import com.th.therp.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@Service
public class DashboardServiceImpl implements DashboardService {

    @Autowired
    private SaleService saleService;

    @Autowired
    private PurchaseService purchaseService;

    @Override
    public DashboardOrderVo order() {
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        // 将 LocalDateTime 转换为字符串（格式为 "yyyy-MM-dd HH:mm:ss.SSS"）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        // 当天开始与结束时间
        String startOfDay = LocalDateTime.of(year, month, day, 0, 0, 0, 0).format(formatter);
        String endOfDay = LocalDateTime.of(year, month, day, 23, 59, 59, 999_999_999).format(formatter);
        // 当年开始与结束时间
        String startOfYear = LocalDateTime.of(year, 1, 1, 0, 0, 0, 0).format(formatter);
        String endOfYear = LocalDateTime.of(year, 12, 31, 23, 59, 59, 999_999_999).format(formatter);
        //构造VO类
        DashboardOrderVo orderVo = new DashboardOrderVo();
        // 查找今日采购订单的数量与金额
        orderVo.setTodayPurchaseQuantity(purchaseService.findQuantityByTime(startOfDay,endOfDay));
        orderVo.setTodayPurchaseAmount(purchaseService.findTotalAmountByTime(startOfDay,endOfDay));
        // 查找本月采购订单的数量与金额
        orderVo.setMonthlyPurchaseQuantity(purchaseService.findQuantityByTime(startOfYear,endOfYear));
        orderVo.setMonthlyPurchaseAmount(purchaseService.findTotalAmountByTime(startOfYear,endOfYear));
        // 查找今日销售订单的数量与金额
        orderVo.setTodaySaleQuantity(saleService.findQuantityByTime(startOfDay,endOfDay));
        orderVo.setTodaySaleAmount(saleService.findTotalAmountByTime(startOfDay,endOfDay));
        // 查找本月销售订单的数量与金额
        orderVo.setMonthlySaleQuantity(saleService.findQuantityByTime(startOfYear,endOfYear));
        orderVo.setMonthlySaleAmount(saleService.findTotalAmountByTime(startOfYear,endOfYear));
        return orderVo;
    }

    @Override
    public List<DashboardMonthVo> month() {
        LocalDate now = LocalDate.now();
        // 获取指定年月的天数
        int year = now.getYear();
        int month = now.getMonthValue();
        int endOfMonth = (YearMonth.of(year, month)).lengthOfMonth();
        // 生成从 1 到 daysInMonth 的列表
        ArrayList<Integer> daysList = new ArrayList<>();
        IntStream.rangeClosed(1, endOfMonth).forEach(x -> daysList.add(x));
        // 打印结果
        System.out.println("天数: " + daysList);
        List<DashboardMonthVo> dashboardMonthVoList = new ArrayList<>();
        // 遍历生成每天的开始时间和结束时间
        for (int day = 1; day <= endOfMonth; day++) {
            // 当天的开始时间 00:00:00.000
            LocalDateTime startOfDay = LocalDateTime.of(year, month, day, 0, 0, 0, 0);
            // 当天的结束时间 23:59:59.999
            LocalDateTime endOfDay = LocalDateTime.of(year, month, day, 23, 59, 59, 999_000_000);
            // 将 LocalDateTime 转换为字符串（格式为 "yyyy-MM-dd HH:mm:ss.SSS"）
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
            String startStr = startOfDay.format(formatter);
            String endStr = endOfDay.format(formatter);
            DashboardMonthVo dashboardMonthVo = saleService.findDashboardMonthVoByTime(startStr,endStr);
            if(dashboardMonthVo != null){
                dashboardMonthVo.setDate(day);
                dashboardMonthVoList.add(dashboardMonthVo);
            }else{
                DashboardMonthVo emptyDashboardMonthVo = new DashboardMonthVo();
                emptyDashboardMonthVo.setDate(day);
                emptyDashboardMonthVo.setQuantity(0);
                emptyDashboardMonthVo.setTotalAmount(0.0);
                dashboardMonthVoList.add(emptyDashboardMonthVo);
            }
        }
        return dashboardMonthVoList;
    }
}